/**
 * @file service-worker.js with workbox api
 * @desc [example](https://workbox-samples.glitch.me/examples/workbox-sw/)
 * @author BQ-DN-XSQ-349(flower.cheng@boqii.com)
 */

/* globals workbox */
workbox.core.setCacheNameDetails({
  prefix: 'boqii-cache',
  suffix: 'v1',
  precache: 'install-time',
  runtime: 'run-time'
});
workbox.skipWaiting();
workbox.clientsClaim();
console.log("self.__precacheManifest", self.__precacheManifest)
workbox.precaching.precacheAndRoute(self.__precacheManifest || []);

/**
 * example runningCache with api
 */
workbox.routing.registerRoute(/^http:\/\/localhost:8585\/(nodeProxy|phpProxy)/,
  workbox.strategies.networkFirst());

workbox.routing.registerRoute(/^https:\/\/(api-dev|api)\.boqii\.com/i,
  workbox.strategies.networkFirst());
/**
 * example runningCache with resources from CDN
 * including maxAge, maxEntries
 * cacheableResponse is important for CDN
 */
workbox.routing.registerRoute(/^https:\/\/cdnjs\.cloudflare\.com/i,
  workbox.strategies.cacheFirst({
    cacheName: 'boqii-lib',
    plugins: [
      new workbox.expiration.Plugin({
        maxEntries: 100,
        maxAgeSeconds: 7 * 24 * 60 * 60
      }),
      new workbox.cacheableResponse.Plugin({
        statuses: [0, 200]
      })
    ]
  })
);
workbox.routing.registerRoute(/^https:\/\/oi2g8z4yp\.qnssl\.com/i,
  workbox.strategies.cacheFirst({
    cacheName: 'boqii-app-bridge',
    plugins: [
      new workbox.expiration.Plugin({
        maxEntries: 100,
        maxAgeSeconds: 7 * 24 * 60 * 60
      }),
      new workbox.cacheableResponse.Plugin({
        statuses: [0, 200]
      })
    ]
  })
);

workbox.routing.registerRoute(/^https:\/\/(api-dev|api|imgtest|img-new|h5|img)\.boqiicdn\.com/i,
  workbox.strategies.cacheFirst({
    cacheName: 'boqii-images-1',
    plugins: [
      new workbox.expiration.Plugin({
        maxEntries: 100,
        maxAgeSeconds: 7 * 24 * 60 * 60
      }),
      new workbox.cacheableResponse.Plugin({
        statuses: [0, 200]
      })
    ]
  })
);

workbox.routing.registerRoute(/favicon.ico/i,
  workbox.strategies.cacheFirst({
    cacheName: 'boqii-images-1',
    plugins: [
      new workbox.expiration.Plugin({
        maxEntries: 100,
        maxAgeSeconds: 7 * 24 * 60 * 60
      }),
      new workbox.cacheableResponse.Plugin({
        statuses: [0, 200]
      })
    ]
  })
);

workbox.routing.registerRoute(
  new RegExp('.*\.css'),
  workbox.strategies.staleWhileRevalidate({
    // Use a custom cache name
    cacheName: 'css-cache'
  })
);

workbox.routing.registerRoute(
  /\/*\//,
  workbox.strategies.networkFirst(),
  'GET'
);
